???.methods.setSectionHeight   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
export default {
2
    data: function () {
3
        return {
4
            /**
5
             * Heights of form fields, keyed by id.
6
             *
7
             * @type {Object}
8
             */
9
            heights: {},
10
        }
11
    },
12
13
    methods: {
14
        /**
15
         * Set heights for form and display elements.
16
         */
17
        setHeights () {
18
            let sections = document.querySelectorAll('.section');
19
20
            for (var i = 0; i < sections.length; i++) {
21
                this.setSectionHeight(sections[i]);
22
            }
23
        },
24
25
        /**
26
         * Set height for single form/display element.
27
         *
28
         * @param {Element} section Form element to set.
29
         */
30
        setSectionHeight (section) {
31
            var id = section.id;
32
33
            let display = this.getDisplayById(id);
34
35
            if (id === 'name' || id === 'projectType') {
36
                this.heights[id] = section.offsetTop + 24;
37
            } else {
38
                this.heights[id] = section.offsetTop + 46;
39
            }
40
41
            this.setPaddingBottom(section, display);
42
        },
43
44
        /**
45
         * Get a display element from the form section id.
46
         *
47
         * @param  {String} id Form section ID.
48
         *
49
         * @return {Element}
50
         */
51
        getDisplayById (id) {
52
            let displayId = 'display'+this.capitalizeFirstLetter(id);
53
54
            return document.getElementById(displayId);
55
        },
56
57
        /**
58
         * Set the bottom padding on a form element if the corresponding
59
         * display element is longer.
60
         *
61
         * @param {Element} section Form section element.
62
         * @param {Element} display Display element.
63
         */
64
        setPaddingBottom (section, display) {
65
            if (section && display) {
66
                let sectionHeight = section.offsetHeight;
67
68
                let displayHeight = display.offsetHeight;
69
70
                let difference = displayHeight - sectionHeight + 30;
71
72
                if (displayHeight > sectionHeight && difference > 15) {
73
                    section.style.paddingBottom = difference + 'px';
74
                }
75
            }
76
        },
77
78
        /**
79
         * Get form element registered from heights object.
80
         *
81
         * @param  {String} key Key for heights array.
82
         *
83
         * @return {String}    Height in px.
84
         */
85
        getHeight (key) {
86
            this.setHeights();
87
            
88
            return this.heights[key];
89
        }
90
    }
91
};